package com.example.drugaiservice.controller;

import com.baidubce.qianfan.Qianfan;
import com.example.drugaiservice.common.result.Result;
import com.example.drugaiservice.pojo.entity.Case;
import com.example.drugaiservice.pojo.entity.Hospital;
import com.example.drugaiservice.pojo.entity.Message;
import com.example.drugaiservice.service.AskService;
import com.example.drugaiservice.service.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

@CrossOrigin
@RestController
@Slf4j
@RequiredArgsConstructor
public class ChatController {

    private final OllamaChatClient chatClient;
    private final AskService askService;
    private final Qianfan qianfan;
    private final RedisService redisService;


    //这个是症状查询的接口，传入一个String类型字符串并返回结果
    @PostMapping("/ai/ask")
    public Result<Message> generate(@RequestBody Message msg) {
        log.info("{}",msg.getMessage());
        String msgs = msg.getMessage() + "请你给我一些建议";
        redisService.rankAddWithZSet(msg.getMessage());
        return Result.success();
        //return Result.success(Message.builder().message(chatClient.call(msgs)).build());
    }

    @PostMapping(value = "/ai/generateStream")
    public Flux<ChatResponse> generateStream(@RequestBody Message message) {
        log.info("传来的信息是：{}", message.getMessage());
        String msg = message.getMessage() + "请你给我一些建议";
        Prompt prompt = new Prompt(new UserMessage(msg));
        return chatClient.stream(prompt);
    }


    //以下为使用千帆大模型完成api的流式处理
    @PostMapping("/ai/qianfan")
    public Iterator<com.baidubce.qianfan.model.chat.ChatResponse> qianfantest(@RequestBody String str) {
        log.info("传来的信息是：{}", str);
        return qianfan.chatCompletion().addMessage("user", str).executeStream();
    }
    @PostMapping("/ai/caseAiSearch")
    public Iterator<com.baidubce.qianfan.model.chat.ChatResponse> caseAiSearch(@RequestBody Case fCase) throws Exception {
        log.info("传来的信息是：{}", fCase.getName());
        String filePath = "files\\" + fCase.getName();
        Path path = Paths.get(filePath);
        byte[] fileContent = Files.readAllBytes(path);
        Case myCase = askService.findPandReturnCase(fileContent);
        return qianfan.chatCompletion()
                .addMessage("user", myCase.getDesc() + "以上是我的化验单，请你帮我简要总结一下异常情况与重点问题")
                .executeStream();
    }
    @PostMapping("/ai/caseAiSearchByphoto")
    public Iterator<com.baidubce.qianfan.model.chat.ChatResponse> caseAiSearchByphoto(MultipartFile file) throws Exception {
        byte[] bytes = file.getBytes();
        Case myCase=askService.findPandReturnCase(bytes);
        return qianfan.chatCompletion()
                .addMessage("user", myCase.getDesc() + "以上是我的化验单，请你帮我简要总结一下异常情况与重点问题")
                .executeStream();
    }
    @PostMapping("/ai/hospitalSearch")
    public Iterator<com.baidubce.qianfan.model.chat.ChatResponse> hospitalSearch(MultipartFile file) throws Exception {
        byte[] bytes = file.getBytes();
        Hospital hospital=askService.findPandReturnHospital(bytes);
        return qianfan.chatCompletion()
                .addMessage("user", hospital.getDesc() + "以上是医院的信息，介绍以上医院的地址和相关的信息")
                .executeStream();
    }
}